/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */
package org.dbwebx.service.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbwebx.domain.Bundle;
import org.dbwebx.service.dao.BundleService;
import org.dbwebx.service.dao.impl.base.BaseService;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * The {@link BundleService} implementation. Provides methods to manage
 * {@link Bundle}s.
 * 
 * @author Izzet_Mustafayev
 * 
 */
@SuppressWarnings("unchecked")
@Repository("bundleService")
@Transactional
public class BundleServiceImpl extends BaseService implements BundleService {

	/**
	 * Case for mapping {@link Bundle}s excluding
	 */
	private static final String MC_BUNDLE_WITHOUT_DBUNITS = "bundleWithoutDbUnits";

	/**
	 * The logger instance.
	 */
	private static final Log logger = LogFactory
			.getLog(BundleServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dbwebx.service.dao.BundleService#find(java.lang.String)
	 */
	public Bundle find(final String bundleName) {
		Bundle bundle = null;

		if (null != bundleName) {
			org.dbwebx.persistence.Bundle foundBundle = findBundleByName(bundleName);

			if (null != foundBundle) {
				bundle = dozerMapper.map(foundBundle, Bundle.class);
			}
		}
		return bundle;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dbwebx.service.dao.BundleService#list()
	 */
	public List<Bundle> list() {
		List<Bundle> bundles = new ArrayList<Bundle>();
		List<org.dbwebx.persistence.Bundle> bundleEntities;
		bundleEntities = em.createQuery("FROM Bundle").getResultList();

		bundles = mapList(bundleEntities, Bundle.class,
				MC_BUNDLE_WITHOUT_DBUNITS);

		return bundles;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dbwebx.service.dao.BundleService#load(java.lang.Long)
	 */
	public Bundle load(final Long bundleId) {
		Bundle bundle = null;

		if ((null != bundleId) && (bundleId > 0)) {
			bundle = dozerMapper.map(em.find(
					org.dbwebx.persistence.Bundle.class, bundleId),
					Bundle.class);
		}

		return bundle;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.dbwebx.service.dao.BundleService#persist(org.dbwebx.domain.Bundle)
	 */
	public Bundle persist(final Bundle bundle) {
		Bundle persistedBundle = null;
		if (null != bundle) {
			persistedBundle = dozerMapper.map(em.merge(dozerMapper.map(bundle,
					org.dbwebx.persistence.Bundle.class)), Bundle.class);
		}
		return persistedBundle;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dbwebx.service.dao.BundleService#remove(java.lang.String)
	 */
	public boolean remove(final String bundleName) {
		boolean result = false;
		if (null != bundleName) {
			org.dbwebx.persistence.Bundle foundBundle = findBundleByName(bundleName);
			if (null != foundBundle) {
				em.remove(foundBundle);
				result = true;
			}
		}
		return result;
	}

	/**
	 * Finds {@link Bundle} by given name.
	 * 
	 * @param bundleName
	 *            the name to find {@link Bundle}
	 * @return found {@link Bundle}
	 */
	private org.dbwebx.persistence.Bundle findBundleByName(
			final String bundleName) {
		org.dbwebx.persistence.Bundle foundBundle = null;
		try {
			foundBundle = (org.dbwebx.persistence.Bundle) em.createNamedQuery(
					org.dbwebx.persistence.Bundle.FIND_BY_NAME).setParameter(
					"bundleName", bundleName).getSingleResult();
		} catch (NoResultException exc) {
			logger.debug("The entity was not found.", exc);
		}

		return foundBundle;
	}

}
